package com.fswan.permission.service;

import com.fswan.permission.Account;
import com.fswan.permission.Role;
import com.fswan.permission.Rule;
import java.util.List;
import java.util.Map;
public interface PermissionService {
    List<Integer> getRolesByAccount(int accountId);
    List<Integer> getRulesByRole(int... rules);
    void register(Rule rule);
    void register(Role role);
    void register(Account account);
    Rule getRuleById(int ruleId);
    Role getRoleById(int roleId);
    Account getAccountById(int accountId);
    default boolean check(int accountId, int ruleId, Map<?, ?> extraData) {
        List<Integer> rolesByAccount = getRolesByAccount(accountId);
        for (Integer roleId : rolesByAccount) {
            List<Integer> rulesByRole = getRulesByRole(roleId);
            if (!rulesByRole.contains(ruleId))
                continue;
            if (getRuleById(ruleId).test(getAccountById(accountId), getRoleById(roleId), extraData))
                return true;
        }
        return false;
    }
}
